package com.mornd.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mornd.gulimall.apis.coupon.to.SeckillSessionTo;
import com.mornd.gulimall.apis.coupon.to.SeckillSkuRelationTo;
import com.mornd.gulimall.coupon.entity.SeckillSession;
import com.mornd.gulimall.coupon.entity.SeckillSkuRelation;
import com.mornd.gulimall.coupon.mapper.SeckillSessionMapper;
import com.mornd.gulimall.coupon.service.ISeckillSessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mornd.gulimall.coupon.service.ISeckillSkuRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 秒杀活动场次 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-08-05
 */
@Service
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionMapper, SeckillSession> implements ISeckillSessionService {
    @Resource
    private ISeckillSkuRelationService seckillSkuRelationService;

    @Override
    public IPage<SeckillSession> pageList(SeckillSession session) {
        IPage<SeckillSession> page = new Page<>(session.validPageNum(), session.validPageSize());
        LambdaQueryWrapper<SeckillSession> qw = Wrappers.<SeckillSession>lambdaQuery();
        qw.like(StringUtils.hasText(session.getName()), SeckillSession::getName, session.getName());
        qw.eq(session.getStatus() != null, SeckillSession::getStatus, session.getStatus());
        super.page(page, qw);
        return page;
    }

    @Override
    public List<SeckillSessionTo> latestDaysSeckillSessions(int day) {
        LocalDate now = LocalDate.now();
        LocalDateTime startTime = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(now.plusDays(day), LocalTime.MAX);

        LambdaQueryWrapper<SeckillSession> qw = Wrappers.<SeckillSession>lambdaQuery();
        qw.between(SeckillSession::getStartTime, startTime, endTime);
        qw.orderByAsc(SeckillSession::getStartTime);

        List<SeckillSession> list = super.list(qw);

        return list.stream().map(it -> {
            SeckillSessionTo to = new SeckillSessionTo();
            BeanUtils.copyProperties(it, to);

            // 查询秒杀商品对应的商品id关系
            List<SeckillSkuRelation> relations = seckillSkuRelationService.list(Wrappers.<SeckillSkuRelation>lambdaQuery().eq(SeckillSkuRelation::getPromotionSessionId, it.getId()));

            List<SeckillSkuRelationTo> seckillSkuRelationTos = relations.stream().map(r -> {
                SeckillSkuRelationTo skuRelationTo = new SeckillSkuRelationTo();
                BeanUtils.copyProperties(r, skuRelationTo);
                return skuRelationTo;
            }).collect(Collectors.toList());

            to.setRelationList(seckillSkuRelationTos);
            return to;
        }).collect(Collectors.toList());
    }
}
